package com.zz.parkingsystem.controller.admin;

import com.zz.parkingsystem.dto.ParkDTO;
import com.zz.parkingsystem.entity.Parking;
import com.zz.parkingsystem.service.PCParkService;
import com.zz.parkingsystem.util.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Api(tags = "泊位管理")
@RequestMapping("admin/park")
public class AdminParkController {

    @Resource
    private PCParkService pcParkService;

    @ApiOperation(value = "泊位管理列表",notes = "可以根据泊位编号进行模糊查询")
    @PostMapping("/getAllPark")
    public Result getAllPark(@RequestBody ParkDTO parkDto){
        parkDto.setStartPage((parkDto.getCurrentPage()-1)*parkDto.getPageSize());
        List<Parking> list = pcParkService.getAllPark(parkDto);
        Long total = pcParkService.getCount(parkDto);
        Map map = new HashMap();
        map.put("list",list);
        map.put("total",total);
        return ResultGenerator.genSuccessResult(map);
    }

    @ApiOperation(value = "查询所有泊位信息，对应地磁")
    @GetMapping("/allPark")
    public Result allPark(){
        List<Parking> parkings = pcParkService.selectPark();
        return ResultGenerator.genSuccessResult(parkings);
    }

    @ApiOperation(value = "根据id查询单个泊位信息",notes = "根据id查询对应泊位号")
    @GetMapping("{id}")
    public Result getParkOne(@PathVariable("id") Long id){
        Parking parking = pcParkService.selectByPrimaryKey(id);
        return ResultGenerator.genSuccessResult(parking);
    }

    @ApiOperation(value = "增加泊位信息",notes = "对应前端的新增")
    @PostMapping("")
    public Result addPark(@RequestBody Parking parking){
        int i = pcParkService.insertSelective(parking);
        return ResultGenerator.genSuccessResult("添加泊位信息成功");
    }

    @ApiOperation(value = "修改泊位信息")
    @PutMapping("")
    public Result updatePark(@RequestBody Parking parking){
        int i = pcParkService.updateByPrimaryKeySelective(parking);
        return ResultGenerator.genSuccessResult("修改信息成功");
    }

    @ApiOperation(value = "删除泊位信息",notes = "根据id删除泊位信息")
    @DeleteMapping("{id}")
    public Result delPark(@PathVariable("id") Long id){
        int i = pcParkService.deleteByPrimaryKey(id);
        return ResultGenerator.genSuccessResult("删除信息成功");
    }

    @Resource
    private OssUtil ossUtil;//引入工具类

    @ApiOperation(value = "上传图片")
    @PostMapping("/uploadoss")
    public Result fileUpload(@RequestParam("file") MultipartFile file) {
        Result result = new Result();
        try {
            String url = ossUtil.uploadFile(file); //调用OSS工具类
            result.setCode(200);
            result.setMessage(url);
            return result;
        } catch (Exception e) {
            result.setCode(400);
            result.setMessage("上传失败");
            return result;
        }
    }

    @ApiOperation(value = "导出泊位信息excel")
    @RequestMapping(value = "/excel")
    public ModelAndView exportExcel() {

        ParkDTO parkDto = new ParkDTO();
        ModelAndView mv = new ModelAndView();
        PageData pd = new PageData();
        try {
            Map<String, Object> dataMap = new HashMap<>();
            List<String> titles = new ArrayList<>();
            titles.add("泊位编号"); // 1
            titles.add("泊位状态"); // 2
            titles.add("激活时间"); // 3
            titles.add("创建时间"); // 4
            dataMap.put("titles", titles);
            List<Parking> varOList = pcParkService.getAllParkExcel(parkDto);
            List<PageData> varList = new ArrayList<>();
            for (int i = 0; i < varOList.size(); i++) {
                PageData vpd = new PageData();
                vpd.put("var1", varOList.get(i).getPkNo()); // 1
                vpd.put("var2", varOList.get(i).getPkStatus()); // 2
                vpd.put("var3", varOList.get(i).getPkActiveTime()); // 3
                vpd.put("var4", varOList.get(i).getPkCreatedTime()); // 4
                varList.add(vpd);
            }
            dataMap.put("varList", varList);
            ObjectExcelView erv = new ObjectExcelView();
            mv = new ModelAndView(erv, dataMap);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return mv;
    }
}
